home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Drivers / Uforce_v1_00.lha / NES.c < prev    next >
C/C++ Source or Header  |  1992-03-24  |  4KB  |  143 lines

  1. /*
  2.  
  3.  NES.c - read a single byte from a standard NES controller
  4.  
  5.  Ethan Dicks    <erd@kumiss.UUCP>
  6.  
  7.  Version 1.0  24-Mar-1992
  8.  
  9.  
  10.  This code is a slightly repackaged version of both the Amiga PowerGlove
  11.  code and the original Atari lores PowerGlove code.  It still does not
  12.  properly request the parallel port from the OS, so use it at your
  13.  own risk.  The original comment block from glovehack.c is included
  14.  below, typos and all.
  15.  
  16.  I AM NOT RESPONSIBLE FOR YOUR HARDWARE.  USE THIS AT YOUR OWN RISK
  17.  NO WARRANTY IS EXPRESSED OR IMPLIED.  USE NO HOOKS.  POST NO BILLS.
  18.  
  19. */
  20.  
  21.  
  22. /**********************************************************************
  23.  
  24.     This ugly hack is based on the ATARI 1040ST power glove
  25.     hack by  manfredo@opal.cs.tu-berlin.de.
  26.  
  27.      This program is without any WARRANTY use at your OWN risk
  28.         @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  29.  
  30.     This Amiga hack was done by Alan Bland.  It is not directly
  31.     compatible with the AC Tech hack, the BYTE hack, nor the
  32.     ATARI 1040ST hack, but you should be able to modify the
  33.     code to work with any of those hacks.
  34.  
  35.     The Amiga code is ugly, as was the original ST code.  The
  36.     parallel port hardware is accessed directly without knowledge
  37.     of the operating system.
  38.  
  39.     Connect NINTENDO POWERGLOVE to Amiga parallel port
  40.           ------
  41.          /   1 |
  42.     | 5  2 |
  43.     | 6  3 |
  44.     | 7  4 |
  45.     --------
  46.  
  47.     GLOVE                       AMIGA
  48.     =====                       =====
  49.  
  50.     GND pin1                    pin18 parallel port
  51.  
  52.     clock pin2                  pin2  parallel port
  53.  
  54.      latch pin3                  pin3  parallel port
  55.  
  56.     data pin4                   pin4  parallel port
  57.  
  58.     +5V pin7                    pin14 power +5V
  59.  
  60.  
  61. **********************************************************************/
  62.  
  63.  
  64. #include <exec/types.h>
  65. #include <exec/ports.h>
  66. #include <exec/memory.h>
  67. #include <hardware/cia.h>
  68. #include <resources/cia.h>
  69.  
  70. #include <proto/exec.h>
  71.  
  72. #include "timer.h"
  73. #include "NES.h"
  74.  
  75. /* bits from parallel port -- Alan's hack */
  76. #define        GDATA           0x04    /* glove data in */
  77. #define        GLATCH          0x02    /* glove latch out */
  78. #define        GCLOCK          0x01    /* glove clock out */
  79. #define        GCLOLAT         (GLATCH|GCLOCK) /* latch and clock */
  80.  
  81. #define getbit()      (ciaa.ciaprb & GDATA) >> 2
  82. #define initport()    ciaa.ciaddrb = GCLOLAT
  83.  
  84. #define     C0L0()       ciaa.ciaprb = 0        /* clock 0 latch 0 */
  85. #define     C0L1()       ciaa.ciaprb = GLATCH   /* clock 0 latch 1 */
  86. #define     C1L0()       ciaa.ciaprb = GCLOCK   /* clock 1 latch 0 */
  87. #define     C1L1()       ciaa.ciaprb = GCLOLAT  /* clock 1 latch 1 */
  88.  
  89. #define     setporta()    delay(3)
  90. #define     setportb()  delay(3)
  91.  
  92. /* convert microseconds to cia ticks */
  93. #define delay(usec) timersleep((usec*1397)/1000)
  94.  
  95. int control_c()
  96. {
  97.     closetimer();
  98.     printf("<<goodbye>>\n");
  99.     return 1; /* causes exit */
  100. }
  101.  
  102. UBYTE query_NES()
  103. {
  104.     register unsigned char Data = 0;
  105.     register int i;
  106.  
  107.     setportb();
  108. /*
  109.  * First, send a RESET pulse (L-H-L), a minimum of 4 uSec long.
  110.  */
  111.     C1L0();
  112.     delay(5);            /* 5 us delay */
  113.     C1L1();
  114.     delay(5);            /* 5 us delay */
  115.     C1L0();
  116.  
  117. /*
  118.  * Now, CLOCK (L-H-L) the data in, at a minimum of 3 uSec per clock pulse
  119. */
  120.     for (i = 0; i < 8; i++) {
  121.         setporta ();    /* configure port a as input */
  122.         Data <<= 1;    /* read a bit */
  123.         Data |= getbit();
  124.         setportb ();    /* prepare port b as output port */
  125.         C0L0 ();    /* generate a clock pulse */
  126.         delay(3);
  127.         C1L0 ();
  128.         delay(3);
  129.     }
  130.     return ( (unsigned char)(Data & 0377));
  131. }
  132.  
  133.  
  134. void init_NES()
  135. {
  136.  
  137.     opentimer();
  138.     onbreak(control_c);
  139.  
  140.     /* initialize hardware interface */
  141.     initport();
  142. }
  143.